-- 计算攻击伤害

-- 内部函数
local function ATTACK_DAMAGE_ONE(source, target, attack, percent, point)
    --需要抽取的附加属性对象，如果是宠物需要附加玩家上去
    local sources = FormulaM.getPropTargets(source);
    local targets = FormulaM.getPropTargets(target);
    
    -- 1: 攻击提升N%
    local prop1 = FormulaM.combineProp(sources, 1);
    
    -- 4~8: X属性出战英雄攻击提升N%
    local prop2 = FormulaM.combinePropByAttr(sources, source.attr, { 4, 5, 6, 7, 8 });
    
    -- 28~35: X系出战影响攻击提升N%
    local prop3 = FormulaM.combinePropByStyle(sources, source.style, { 28, 29, 30, 31, 32, 33, 34, 35 });
    
    -- 百分比提升先计算
    prop1[2] = prop1[2] + prop2[2] + percent;
    attack = PropM.apply(prop1, attack);

    -- 300 : 攻击提升N点
    prop1 = FormulaM.combineProp(sources, 300);
    attack = PropM.apply(prop1, attack) + point;
    
    -- 303~307: X属性出战英雄攻击增加N点
    prop1 = FormulaM.combinePropByAttr(sources, source.attr, { 303, 304, 305, 306, 307 });
    attack = PropM.apply(prop1, attack);
    
    -- 338~342: 目标是X属性时，伤害增加N点
    prop1 = FormulaM.combinePropByAttr(sources, target.attr, { 338, 339, 340, 341, 342 }, false);
    attack = PropM.apply(prop1, attack);
    
    -- 336: 攻击减低N点
    prop3 = FormulaM.combineProp(sources, 336);
    attack = PropM.apply(prop3, attack);
    
    -- 到这里，得到了攻击者因为附加属性影响后的攻击值
    -- 然后开始防守者的附加属性影响
    
    -- 19: 伤害减少N%
    prop3 = FormulaM.combineProp(targets, 19);
    attack = PropM.apply(prop3, attack);
    
    -- 318: 伤害减少N点
    prop3 = FormulaM.combineProp(targets, 318);
    attack = PropM.apply(prop3, attack);
    
    -- 319~323: X属性出战英雄伤害减少N点
    prop1 = FormulaM.combinePropByAttr(targets, target.attr, { 319, 320, 321, 322, 323 }, false);
    attack = PropM.apply(prop1, attack);

    --计算完毕
    return attack;

end

-- percent为提升的千分比
-- point为提升的攻击点数
return function(source, target, attack, percent, point)
    if target.type == OBJECT_TYPE_MONSTER then
        -- 我攻击怪物，那么比较简单，目标target即为怪物一个人
        return ATTACK_DAMAGE_ONE(source, target, attack, percent, point);
    end 
    
    -- 怪物攻击我，那么target应该是所有宠物作为一个整体来计算
    local damage = 0;
    for _, f in pairs(_G.templet:getFriends(target)) do
        if f.type ~= OBJECT_TYPE_USER then
            damage = damage + ATTACK_DAMAGE_ONE(source, f, attack, percent, point);
        end
    end
    return damage;
end